﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.eforceglobal.crux.controllers.Administration;
using com.eforceglobal.crux.DTOs;
using com.eforceglobal.crux.views.Administration;
using com.eforceglobal.crux.controllers;

public partial class Administration_AssignCompaniesToUser : BaseView, IAssignCompanyToUser
{
    AssignCompaniesToUserController _controller;

    protected void Page_Load(object sender, EventArgs e)
    {
        _controller = new AssignCompaniesToUserController(this);
        new SessionData();
        _controller.InitView(IsPostBack);
        userid = int.Parse(drpallusers.SelectedValue);
        if (!Page.IsPostBack)
        {
            #region Site Map
            UrlTreeNodeItems.SetSiteNavigation(PageSiteMap, UrlTreeNodeItems.AssignCompanyToUser);
            #endregion
        }
        
    }

    public List<CompanyInformation> AllowedCompanies
    {
        get { throw new NotImplementedException(); }
        set {
            chkCompaniesToAssign.DataSource = value;
            chkCompaniesToAssign.DataTextField = "CompanyName";
            chkCompaniesToAssign.DataValueField = "CompanyID";
            chkCompaniesToAssign.DataBind();
        }
    }

    public List<CompanyInformation> AssignedCompanies
    {
        get { return GetSelectedCompanies(); }
        set {
            PopulateAssignedCompanies(value);
        }
    }

    public List<CompanyInformation> GetSelectedCompanies()
    {
        List<CompanyInformation> companies = new List<CompanyInformation>();
        foreach (ListItem item in chkCompaniesToAssign.Items)
        {
            if(item.Selected)
            {
                CompanyInformation companyinformation = new CompanyInformation();
                companyinformation.CompanyID = int.Parse(item.Value);
                companies.Add(companyinformation);
            }
        }
        return companies;
    }

    private void PopulateAssignedCompanies(List<CompanyInformation> companies)
    {
        foreach (ListItem item in chkCompaniesToAssign.Items)
        {
            if (companies.SingleOrDefault(p => p.CompanyID == int.Parse(item.Value)) != null)
                item.Selected = true;
            else item.Selected = false;
        }
    }

    public int userid
    {
        get { return int.Parse(drpallusers.SelectedValue); }
        set { drpallusers.SelectedValue = userid.ToString(); }
    }

    public List<UserInformation> AllUsers
    {
        set
        {
            SetAllUsers(value);
        }
    }

    public bool isSaveAllowed
    {
        get { return btnSubmit.Visible; }
        set { btnSubmit.Visible = value; }
    }

    private void SetAllUsers(List<UserInformation> value)
    {
        drpallusers.Items.Clear();
        foreach (var user in value)
        {
            ListItem item = new ListItem();
            item.Value = user.UserID.ToString();
            item.Text = user.UserEmailAddress;
            drpallusers.Items.Add(item);
        }
       
    }
    protected void drpallusers_SelectedIndexChanged(object sender, EventArgs e)
    {
        string strUserid = drpallusers.SelectedValue;
        int selecteduserid =  int.Parse(strUserid);
        _controller.PopulateAssignedCompanies(selecteduserid);
    }
    protected void btnSumit_Click(object sender, EventArgs e)
    {
        _controller.SaveCompanySettingsForUser();
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        NavigationController.OpenView("Administration");
    }
}